<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false" style="background: #fff">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-collapse v-model="activeKey">
          <a-collapse-panel key="1" header="基本信息">
            <a-row :gutter="48">
              <a-col :span="8" :hidden="true">
                <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bpmBizTitle">
                  <a-input
                    :disabled="formDisabled"
                    v-model="model.bpmBizTitle"
                    placeholder="请输入标题"
                    :max-length="255"
                  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="单据编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="billNo">
                  <a-input v-model="model.billNo" placeholder="请输入单据编号" disabled></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="申请人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="employeeId">
                  <!-- <a-input v-if="formDisabled || showFlowSubmitButton" readOnly @click.prevent="goEmployeeId(model.employeeId)"  v-model="model.employeeId_dictText" style="color: #1890FF; cursor: pointer;"  placeholder="请选择申请人"></a-input> -->
                  <select-user-by-dep :multi="false" v-model="model.employeeId" disabled></select-user-by-dep>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="人员编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="employeeNo">
                  <a-input :disabled="formDisabled" v-model="model.employeeNo" placeholder="请输入人员编号" disabled></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gender">
                  <j-dict-select-tag
                    type="list"
                    v-model="model.gender"
                    dictCode="gender"
                    placeholder="请选择性别"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="出生日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="birthday">
                  <j-date placeholder="请选择出生日期" v-model="model.birthday" style="width: 100%" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="岗位序列" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="positionCategory">
                  <j-dict-select-tag
                    type="list"
                    v-model="model.positionCategory"
                    dictCode="position_category"
                    placeholder="请选择岗位名称"
                    disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="岗位名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="positionId">
                  <j-dict-select-tag
                    type="list"
                    v-model="model.positionId"
                    dictCode="sys_position,name,id,del_flag = 0 and audit_status=2"
                    placeholder="请选择岗位名称"
                    disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="毕业院校" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="graduatedFrom">
                  <a-input :disabled="formDisabled" v-model="model.graduatedFrom" placeholder="请输入毕业院校" disabled></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="学历" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="highestDegree">
                  <j-dict-select-tag
                    type="list"
                    v-model="model.highestDegree"
                    dictCode="highest_degree"
                    placeholder="请选择学历"
                    disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="所学专业" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="major">
                  <a-input :disabled="formDisabled" v-model="model.major" placeholder="请输入专业" disabled></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="毕业时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="graduationDate">
                  <j-date
                    placeholder="请选择毕业时间"
                    v-model="model.graduationDate"
                    style="width: 100%"
                    disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="所在部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departmentId">
                  <j-select-depart v-model="model.departmentId" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="入职日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="entryTime">
                  <j-date
                    placeholder="请选择入职日期"
                    v-model="model.entryTime"
                    style="width: 100%"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="职称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="professionalTitle">
                  <a-input :disabled="formDisabled" v-model="model.professionalTitle" placeholder="请输入职称" disabled></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="职业资格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="professionalQualification">
                  <a-input :disabled="formDisabled || showFlowSubmitButton" v-model="model.professionalQualification" placeholder="请输入职业资格"></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="所属团队" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="teamId">
                  <select-team-input v-model="model.teamId" :tdlx="3" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="非相关工作经验（年）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workExperience">
                  <a-input :disabled="formDisabled || showFlowSubmitButton" v-model="model.workExperience" placeholder="请输入非相关工作经验（年）"></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="相关工作经验（年）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="relatedWorkExperience">
                  <a-input :disabled="formDisabled || showFlowSubmitButton" v-model="model.relatedWorkExperience" placeholder="相关工作经验（年）"></a-input>
                </a-form-model-item>
              </a-col>
              <!-- <a-col :span="8">
                <a-form-model-item label="试用期限" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="probationership">
                  <j-dict-select-tag
                    :disabled="formDisabled"
                    type="list"
                    v-model="model.probationership"
                    dictCode="probationership"
                    placeholder="请选择试用期限"
                  />
                </a-form-model-item>
              </a-col> -->
              <a-col :span="8">
                <a-form-model-item label="申请转正日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="regularTime">
                  <!-- <j-date
                    :disabled="formDisabled || showFlowSubmitButton"
                    placeholder="请选择申请转正日期"
                    v-model="model.regularTime"
                    :disabled-date="disabledDate"
                    :default-picker-value="this.model.entryTime"
                    style="width: 100%"
                  /> -->
                  <a-date-picker
                    :disabled="formDisabled || showFlowSubmitButton"
                    :disabled-date="disabledDate"
                    :default-picker-value="this.model.entryTime"
                    placeholder="请选择申请转正日期"
                    valueFormat="YYYY-MM-DD"
                    v-model="model.regularTime"
                    style="width: 100%" />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="试用期工资标准（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="salary">
                  <a-input-number
                    :disabled="formDisabled || !showFlowSubmitButton"
                    :max="99999999"
                    :min="0"
                    style="width: 100%"
                    v-model="model.salary"
                    placeholder="试用期工资标准（元）" ></a-input-number>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="预计转正后薪级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="regularScale">
                  <j-dict-select-tag
                    :disabled="formDisabled || !showFlowSubmitButton"
                    type="list"
                    v-model="model.regularScale"
                    dictCode="erp_pay_grade,salary_grade,id,del_flag = 0 and audit_status=2 order by sort"
                    placeholder="请选择预计转正后薪级"

                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="转正后确认薪级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="salaryScale">
                  <j-dict-select-tag
                    :disabled="formDisabled || !showFlowSubmitButton"
                    type="list"
                    v-model="model.salaryScale"
                    dictCode="erp_pay_grade,salary_grade,id,del_flag = 0 and audit_status=2 order by sort"
                    placeholder="请选择转正后确认薪级"

                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="公积金基数（元）"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="providentFundBase"
                >
                  <a-input-number
                    :max="99999999"
                    :min="0"
                    :disabled="formDisabled || !showFlowSubmitButton"
                    v-model="model.providentFundBase"
                    placeholder="请输入公积金基数"
                    style="width: 100%"

                  ></a-input-number>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="公积金购买单位"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="providentFundUnit"
                >
                  <select-Company-input :disabled="formDisabled || !showFlowSubmitButton" v-model="model.providentFundUnit" />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="其他约定事项" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
                  <a-textarea :disabled="formDisabled || showFlowSubmitButton" v-model="model.remark" rows="4" placeholder="请输入其他约定事项" :max-length="500"/>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-collapse-panel>
          <!-- <a-collapse-panel key="2" header="员工试用期考核信息" v-if="this.formBpm === true || formDisabled">
            <a-row :gutter="48"> -->
          <!-- <a-col :span="24" style="text-align:center;border:1px dashed #ccc;padding:40px 0px 20px;margin:20px">
                <a-form-model-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="regularDetail">
                  <a style="color:blue;text-decoration:underline;font-weight:bold" @click="goRegularDetail">查看员工试用期考核表</a>
                </a-form-model-item>
              </a-col> -->
          <!-- <a-col :span="12">
                <a-form-model-item label="评价人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="appraiser">
                  <select-user-by-dep :disabled=formDisabled  :multi="false" v-model="model.appraiser"></select-user-by-dep>
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-form-model-item label="评价打分" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="appraiseScore">
                  <j-dict-select-tag
                    :disabled="formDisabled"
                    type="list"
                    v-model="model.appraiseScore"
                    dictCode="appraise_score"
                    placeholder="请选择评价打分"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item
                  label="试用期评价"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="evaluationPeriod"
                >
                  <a-textarea
                    :disabled="formDisabled"
                    v-model="model.evaluationPeriod"
                    rows="4"
                    placeholder="请输入试用期评价"
                    :max-length="500"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item
                  label="需改进事项"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="improvementItems"
                >
                  <a-textarea
                    :disabled="formDisabled"
                    v-model="model.improvementItems"
                    rows="4"
                    placeholder="请输入需改进事项"
                    :max-length="500"
                  />
                </a-form-model-item>
              </a-col> -->
          <!-- </a-row>
          </a-collapse-panel> -->
          <a-collapse-panel key="3" header="附件信息">
            <a-row>
              <a-col :span="24">
                <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachment">
                  <j-upload :disabled="formDisabled || showFlowSubmitButton" v-model="model.attachment"></j-upload>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-collapse-panel>
        </a-collapse>
        <!-- <a-row>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row> -->
      </a-form-model>
      <common-flow-list v-if="formBpm && model.bpmStatus !== '1'" :formData="formData" style="margin-top: 10px"></common-flow-list>
    </j-form-container>
    <a-row :gutter="48">
      <a-col :span="24" style="text-align: center;margin-top: 15px">
        <a-button type="primary" v-if="formBpm" :style="{ marginRight: showFlowSubmitButton?'10px':'0px' }" @click="onPrint">打印</a-button>
        <a-button @click="submitForm('submit')" v-if="showFlowSubmitButton">提 交</a-button>
      </a-col>
    </a-row>
    <process-config-modal ref="processConfig" @ok="handleSuccess" @close="handleClose"></process-config-modal>
    <erp-hr-employee-regular-detail-modal ref="regularDetail" :printShow="printShow" @complete="completeData" @ok="handleDataOk"></erp-hr-employee-regular-detail-modal>

    <ErpHrEmployeeRegularBillFormPrint
      v-if="formBpm && isSubmit"
      id="regularContent"
      class="print_style"
      :formData="model1"
      :formDataHistory="formData"></ErpHrEmployeeRegularBillFormPrint>
    <select-schedule ref="schedule" :empId="employeeId" :applyId="applyId"></select-schedule>
  </a-spin>
</template>

<script>
import { httpAction, getAction, postAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import SelectTeamInput from '@/views/scy/components/SelectTeamInput'
import { mapGetters } from 'vuex';
import moment from 'moment'
import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
import ProcessConfigModal from '@/views/scy/components/ProcessConfigModal'
import SelectCompanyInput from '@/views/scy/components/SelectCompanyInput'
import ErpHrEmployeeRegularDetailModal from './ErpHrEmployeeRegularDetailModal'
import CommonFlowList from '@/views/components/CommonFlowList.vue';
import Print from '@/utils/print/printarea.js';
import ErpHrEmployeeRegularBillFormPrint from './ErpHrEmployeeRegularBillFormPrint'

export default {
  name: 'ErpHrEmployeeRegularBillForm',
  components: {
    SelectTeamInput,
    SelectUserByDep,
    ProcessConfigModal,
    SelectCompanyInput,
    ErpHrEmployeeRegularDetailModal,
    ErpHrEmployeeRegularBillFormPrint,
    CommonFlowList,
    SelectSchedule: () => import('@/views/components/modal/SelectSchedule')
  },
  props: {
    // 流程表单data
    formData: {
      type: Object,
      default: () => {},
      required: false
    },
    // 表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    },
    // 一览表打印按钮显示
    printShow: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    var validatorFlag = async (rule, value, callback) => {
      if (value) {
        const that = this;
        if (!this.flag) {
          callback('请保存试用期考核表！')
          return;
        }
        callback()
      }
      callback()
    };
    return {
      applyId: null,
      employeeId: null,
      flag: false,
      isSubmit: true,
      activeKey: ['1', '2', '3'],
      model: {
        billNo: null
      },
      model1: {
        billNo: null
      },
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 }
      },
      confirmLoading: false,
      validatorRules: {
        relatedWorkExperience: [
          { required: true, message: '请输入相关工作经验!' }
        ],
        regularScale: [
          { required: false, message: '请选择预计转正后薪级!' }
        ],
        professionalQualification: [
          { required: true, message: '请输入职业资格!' }
        ],
        salary: [
          { required: false, pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的试用期工资标准!' }
        ],
        teamId: [
          { required: true, message: '请选择所属团队!' }
        ],
        bpmBizTitle: [
          { required: true, message: '请输入流程标题!' }
        ],
        entryTime: [
          { required: true, message: '您暂无入职日期，请联系HR!' }
        ],
        probationership: [
          { required: true, message: '请选择试用期限!' }
        ],
        regularTime: [
          { required: true, message: '请选择申请转正日期!' }
        ],
        salaryScale: [
          { required: false, message: '请选择薪级!' }
        ],
        providentFundBase: [
          { required: false, pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的公积金基数!' }
        ],
        providentFundUnit: [
          { required: false, message: '请选择公积金购买单位!' }
        ],
        appraiser: [
          { required: true, message: '请选择评价人!' }
        ],
        appraiseScore: [
          { required: true, message: '请选择试用期打分!' }
        ],
        evaluationPeriod: [
          { required: true, message: '请输入试用期评价!' }
        ],
        improvementItems: [
          { required: true, message: '请输入需改进事项!' }
        ]
        // regularDetail: [
        //   { required: true, message: '请完善员工试用期考核表!' },
        //   { validator: validatorFlag },
        // ],
      },
      url: {
        startProcess: '/api/erpmng/act/process/extActProcess/startMutilProcess',
        generatedFormNumber: '/api/erp/base/generatedFormNumber',
        add: '/api/erp/hr/regular/erpHrEmployeeRegularBill/add',
        edit: '/api/erp/hr/regular/erpHrEmployeeRegularBill/edit',
        queryById: '/api/erp/hr/regular/erpHrEmployeeRegularBill/queryById',
        queryEmployeeDataById: '/api/erp/files/employee/erpEmployeeFile/queryById',
        queryEmployeeById: '/api/erp/files/employee/erpEmployeeFile/queryPageById',
        queryPostRecordByInfo: '/api/erp/files/employee/erpEmployeePostChangeRecord/queryPostRecordByInfo',
        queryInductionByEmployeeId: '/api/erp/hr/induction/erpHrInductionBill/queryInductionByEmployeeId'
      },
      // 代码生成后需手动设置流程编码
      flowCode: 'onl_erp_hr_employee_regular_bill',
      formUrl: 'erp/hr/regular/modules/ErpHrEmployeeRegularBillForm',
      formUrlMobile: '/pages/synergy/erpHrEmployeeRegularBillList',
      processId: '1540233415628972033',
      beanName: 'erpHrEmployeeRegularBillServiceImpl'
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          this.initValidatorRules(true);
          return true
        }
      }
      return false
    }
  },
  created() {
    // 备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    // 如果是流程中表单，则需要加载流程表单data
    this.showFlowData();
  },
  methods: {
    generatedFormNumber() {
      getAction(this.url.generatedFormNumber, { prefix: 'ZZ', subSystemCode: '02' }).then(res => {
        if (res.success) {
          this.$set(this.model, 'billNo', res.result)
          this.$set(this.modelDefault, 'billNo', res.result);
        }
      })
    },
    initValidatorRules(boolean) {
      if (boolean) {
        this.$set(this.validatorRules, 'regularScale', [{ required: true, message: '请选择预计转正后薪级!' }]);
        this.$set(this.validatorRules, 'salary', [{ required: true, pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的试用期工资标准!' } ]);
        this.$set(this.validatorRules, 'salaryScale', [{ required: true, message: '请选择薪级!' }]);
        this.$set(this.validatorRules, 'providentFundBase', [{ required: true, pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的公积金基数!' }]);
        this.$set(this.validatorRules, 'providentFundUnit', [{ required: true, message: '请选择公积金购买单位!' }]);
      }
    },
    goEmployeeId(empId) {
      getAction(this.url.queryEmployeeDataById, { id: empId }).then(res => {
        if (res.success) {
          let data = res.result;
          if (data) {
            this.employeeId = empId;
            this.applyId = data.applyId;
            this.$refs.schedule.open();
            this.$refs.schedule.title = '员工档案详情';
          }
        }
      })
    },
    disabledDate(current) {
      return current < moment(this.model.entryTime).subtract('day')
    },
    completeData(data) {
      this.flag = data;
    },
    handleDataOk(data) {
      this.model.regularDetail = Object.assign({}, data);
    },
    ...mapGetters(['userInfo']),
    add() {
      let userinfo = this.userInfo();
      this.generatedFormNumber();
      this.$set(this.modelDefault, 'employeeId', userinfo.empId);
      this.$set(this.modelDefault, 'employeeNo', userinfo.employee.employeeNo);
      this.$set(this.modelDefault, 'applyId', userinfo.employee.applyId);
      // this.$set(this.modelDefault,'workUnit',userinfo.employee.orgDepart);
      this.$set(this.modelDefault, 'departmentId', userinfo.deptId);
      // this.$set(this.modelDefault,'teamId',userinfo.employee.teamId);
      // this.$set(this.modelDefault,'positionId',userinfo.employee.mainPost);
      this.$set(this.modelDefault, 'entryTime', userinfo.employee.joinDate);
      this.$set(this.modelDefault, 'gender', userinfo.employee.gender);
      this.$set(this.modelDefault, 'birthday', userinfo.employee.birthDate);
      this.$set(this.modelDefault, 'graduatedFrom', userinfo.employee.graduatedFrom);
      this.$set(this.modelDefault, 'highestDegree', userinfo.employee.highestDegree);
      this.$set(this.modelDefault, 'major', userinfo.employee.major);
      this.$set(this.modelDefault, 'graduationDate', userinfo.employee.graduationDate);
      this.$set(this.modelDefault, 'professionalTitle', userinfo.employee.professionalTitle);
      this.$set(this.modelDefault, 'bpmBizTitle', userinfo.employee.name);
      this.edit(this.modelDefault);
      getAction(this.url.queryPostRecordByInfo, { employeeId: userinfo.empId, departId: userinfo.deptId }).then(res => {
        if (res.success) {
          let data = res.result;
          if (data) {
            console.log('positionCategory', data)
            this.$set(this.model, 'teamId', data[0].teamId);
            this.$set(this.model, 'positionId', data[0].postCode);
            this.$set(this.model, 'positionCategory', data[0].positionCategory);
          }
        }
      })
    },
    edit(record) {
      this.$refs.form.resetFields()
      this.model = Object.assign({}, record)
      this.model1 = Object.assign({}, record)
      if (this.model.regularDetail) {
        let evaluating = this.model.regularDetail.evaluating
        if (evaluating) {
          this.model.regularDetail.evaluating = JSON.parse(evaluating);
          this.model1.regularDetail.evaluating = JSON.parse(evaluating);
        }
        if (this.model.regularDetail.id) {
          this.flag = true;
        }
      }
      this.visible = true
    },
    // 渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    handleSuccess() {
      this.$emit('ok', 'refresh');
    },
    handleClose () {
      this.$emit('close');
    },
    isJsonString(str) {
      try {
        if (typeof JSON.parse(str) === 'object') {
          return true;
        }
      } catch (e) {
      }
      return false;
    },
    goRegularDetail() {
      let entryTime = this.model.entryTime;
      let regularDetail = this.model.regularDetail;
      if (this.formBpm) {
        if (this.formData) {
          if (this.formData.disabled != undefined) {
            if (this.formData.disabled != false) {
              this.$refs.regularDetail.disableSubmit = true;
            }
          } else {
            this.$refs.regularDetail.disableSubmit = true;
          }
        } else {
          this.$refs.regularDetail.disableSubmit = true;
        }
      }
      if (regularDetail) {
        if (this.disabled) {
          this.$refs.regularDetail.disableSubmit = true;
        }
        if (!this.isJsonString(regularDetail.evaluating)) {
          this.$refs.regularDetail.edit(regularDetail, entryTime);
        } else {
          regularDetail.evaluating = JSON.parse(regularDetail.evaluating)
          this.$refs.regularDetail.edit(regularDetail, entryTime);
        }
      } else {
        if (this.formBpm) {
          if (this.formData) {
            if (this.formData.disabled == undefined) {
              this.$refs.regularDetail.add(null, entryTime);
              return;
            }
          }
        }
        let data = {};
        data.employeeId = this.model.employeeId;
        data.employeeNo = this.model.employeeNo;
        data.gender = this.model.gender;
        data.positionId = this.model.positionId;
        data.positionCategory = this.model.positionCategory;
        data.entryTime = this.model.entryTime;
        getAction(this.url.queryInductionByEmployeeId, { employeeId: this.model.employeeId }).then(res => {
          if (res.success) {
            let result = res.result;
            if (result) {
              let endTime = this.model.entryTime;
              endTime = moment(endTime).add(result.probationership, 'months').format('YYYY-MM-DD');
              data.endTime = endTime;
              this.$refs.regularDetail.add(data, entryTime);
            } else {
              this.$refs.regularDetail.add(data, entryTime);
            }
          }
        })
      }
    },
    // 发起流程
    startProcess: function (id) {
        // var that = this;
        // var param = {
        //   flowCode: that.flowCode,
        //   id: id,
        //   formUrl: that.formUrl,
        //   formUrlMobile: that.formUrlMobile
        // }
        // postAction(that.url.startProcess, param).then((res) => {
        //   if (res.success) {
        //     that.$message.success(res.message);
        //     that.$emit('ok');
        //   } else {
        //     that.$message.warning(res.message);
        //   }
        // }).catch((e) => {
        //   this.$message.warning('不识别的请求!');
        // });
        let record = { id: id };
        this.$refs.processConfig.visible = true
        this.$refs.processConfig.formUrl = this.formUrl
        this.$refs.processConfig.formUrlMobile = this.formUrlMobile
        this.$refs.processConfig.flowCode = this.flowCode
        this.$refs.processConfig.edit(record, '', this.beanName);
    },
    // 保存草稿
    submitTempForm() {
      const that = this
      // 触发表单验证
      // that.confirmLoading = true
      let httpurl = ''
      let method = ''
      if (!this.model.id) {
        httpurl += this.url.add
        method = 'post'
      } else {
        httpurl += this.url.edit
        method = 'put'
      }
      if (this.model.regularDetail) {
        let evaluating = this.model.regularDetail.evaluating
        if (evaluating) {
          this.model.regularDetail.evaluating = JSON.stringify(evaluating);
        }
      }
      httpAction(httpurl, this.model, method)
        .then((res) => {
          if (res.success) {
            that.$message.success(res.message)
            this.$emit('ok', 'draft', this.model);
          } else {
            that.$message.warning(res.message)
            that.$emit('unButtonDisable');
          }
        }).catch((error) => {
        that.$emit('unButtonDisable', error);
      }).finally(() => {
        that.confirmLoading = false;
      })
    },
    submitForm(type) {
      if (type && type == 'draft') {
        this.submitTempForm()
        return false
      }
      const that = this
      this.isSubmit = false
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          if (this.model.regularDetail) {
            let evaluating = this.model.regularDetail.evaluating
            if (evaluating && !this.isJsonString(evaluating)) {
              this.model.regularDetail.evaluating = JSON.stringify(evaluating);
            }
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                let data = res.result;
                if (this.model.id) {
                  if (this.formBpm === true) {
                    this.$emit('ok', 'submit', this.model);
                    that.$message.success(res.message);
                    this.showFlowData()
                  } else {
                    this.startProcess(this.model.id);
                  }
                } else {
                  if (this.formBpm === true) {
                    this.$emit('ok', 'submit', this.model);
                    that.$message.success(res.message)
                  } else {
                    this.startProcess(data);
                    this.model.id = data;
                  }
                }
                if (this.formBpm === true) {
                  if (!this.formData.dataId) return false
                  let params = { id: this.formData.dataId }
                  getAction(this.url.queryById, params).then((res) => {
                    if (res.success) {
                      this.model1 = res.result
                      this.isSubmit = true
                    }
                  })
                }
              } else {
                that.$message.warning(res.message)
                that.$emit('unButtonDisable');
              }
            }).catch(err => {
              that.$emit('unButtonDisable', err);
            })
            .finally(() => {
              that.confirmLoading = false
            })
        } else {
          that.$emit('unButtonDisable');
        }
      })
    },

    // 点击打印
    onPrint () {
        let print = new Print({
          el: '#regularContent',
          popTitle: '转正申请详情 · 禾美环保',
          endCallback() {

          }
        })
      }
  }
}
</script>
<style>
  @import "../../../../../utils/print.css";
</style>
